<template>
	<view class="content">
		<view class="navbar">
			<view 
				v-for="(item, index) in navList" :key="index" 
				class="nav-item" 
				:class="{current: tabCurrentIndex === item.state}"
				@click="tabClick(item.state)"
			>
				{{item.text}}
			</view>
		</view>
			<view v-if="tabCurrentIndex==2">
				<view
					v-for="(item,index) in orderList" :key="index"
					class="order-item"
				>
					<view class="goods-wrap">
						<image class="goods-img" :src="item.image" mode="aspectFill"></image>
						<view class="right">
							<text class="title clamp">{{item.title}}</text>
							
							<view class="price" style="color:#f9436b;padding-top:30rpx;">￥<text style="font-weight:bold;font-size:38rpx;">{{item.price}}</text></view>
						</view>
						<view @click="operate(item,'上架')" class="shangjia" v-if="!item.is_shelves">
							上架
						</view>
						<view  @click="operate(item,'下架')" class="xiajia" v-else>
							下架
						</view>
					</view>
				</view>
			</view>
			<view v-if="tabCurrentIndex==1">
				<view
					v-for="(item,index) in orderList" :key="index"
					class="order-item"
				>
					
					
					
					<view class="goods-wrap goods-line">
						<image class="goods-img" :src="item.image" mode="aspectFill"></image>
						<view class="right">
							<text class="title clamp">{{item.title}}</text>
							
							
							<view style="color:#909399;padding:8rpx 0;">{{item.sku}}</view>
							<view class="price">{{item.price}} X {{item.number}}</view>
						</view>
						
					</view>
					<view class="i-top">
						<text class="time">订单金额</text>
						<text class="state">{{item.total_price}}</text>
						
					</view>	
					
					
					<view class="price-box">
						
						<text class=" price" style="padding:0">收货人:{{item.buyer}}</text>
						
						<text class="price" >电话:{{item.buyer_phone}}</text>
					</view>
						
				</view>
			</view>
			<uni-load-more  :status="more">
			</uni-load-more>
	</view>
</template> 

<script>
	import empty from "@/components/empty";
	export default {
		components: {
			empty
		},
		data() {
			return {
				more:'loading',
				nowpage:1,
				orderList:[],
				tabCurrentIndex: 1,
				navList: [{
						state: 1,
						text: '订单管理',
						loadingType: 'more',
						orderList: []
					},
					{
						state: 2,
						text: '商品管理',
						loadingType: 'more',
						orderList: []
					}
					
				],
			};
		},
		
		onLoad(options){
			this.loadData()
			
			
		},
		 
		methods: {
			operate(item,type){
				let that=this
				uni.showModal({
					title: '确定'+type,
					content: '',
					success: function (res) {
						if (res.confirm) {
							
							
							that.apipost('/api/user/goods_shelves',{
								id:item.id
								
							},true).then(res => {
								if(res.Code==10200){
									item.is_shelves=!item.is_shelves
									uni.showToast({
									    title: res.Message,
									    duration:4000,
										icon:'none'
									});
									
								}
								
							}).catch(err=>{
								console.log(err)
							});
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			getshangpin(){
				
				let that=this
				that.apipost('/api/user/shop_goods_list',{
					page:that.nowpage
					
				},true).then(res => {
					if(res.Code==10200){
						let data=res.Data
						that.orderList=that.orderList.concat(data)
						++that.nowpage
						uni.stopPullDownRefresh()
						
						if(data.length<15){
							
							that.more='nomore'
						}
						
					}
					
				}).catch(err=>{
					console.log(err)
				});
			},
			getdingdan(){
				let that=this
				that.apipost('/api/user/shop_order_list',{
					page:that.nowpage
					
				},true).then(res => {
					if(res.Code==10200){
						let data=res.Data
						that.orderList=that.orderList.concat(data)
						++that.nowpage
						uni.stopPullDownRefresh()
						
						if(data.length<15){
							
							that.more='nomore'
						}
						
					}
					
				}).catch(err=>{
					console.log(err)
				});
			},
			loadData(){
				
				if(this.tabCurrentIndex==1){
					this.getdingdan()
				}else{
					this.getshangpin()
				}
				
				
				
			}, 

			
			//顶部tab点击
			tabClick(index){
				this.orderList=[]
				this.nowpage=1
				this.tabCurrentIndex = index;
				this.more='loading'
				this.loadData()
			},
			//删除订单
			deleteOrder(index){
				uni.showLoading({
					title: '请稍后'
				})
				setTimeout(()=>{
					this.navList[this.tabCurrentIndex].orderList.splice(index, 1);
					uni.hideLoading();
				}, 600)
			},
			//取消订单
			cancelOrder(item){
				uni.showLoading({
					title: '请稍后'
				})
				setTimeout(()=>{
					let {stateTip, stateTipColor} = this.orderStateExp(9);
					item = Object.assign(item, {
						state: 9,
						stateTip, 
						stateTipColor
					})
					
					//取消订单后删除待付款中该项
					let list = this.navList[1].orderList;
					let index = list.findIndex(val=>val.id === item.id);
					index !== -1 && list.splice(index, 1);
					
					uni.hideLoading();
				}, 600)
			},

			//订单状态文字和颜色
			orderStateExp(state){
				let stateTip = '',
					stateTipColor = '#fa436a';
				switch(+state){
					case 1:
						stateTip = '待付款'; break;
					case 2:
						stateTip = '待发货'; break;
					case 9:
						stateTip = '订单已关闭'; 
						stateTipColor = '#909399';
						break;
						
					//更多自定义
				}
				return {stateTip, stateTipColor};
			}
		},
	}
</script>

<style lang="scss">
	.shangjia{
		display:flex;
		justify-content: center;
		align-items: center;
		border-radius:12rpx;
		position:absolute;
		right:12rpx;
		bottom:20rpx;
		width:100rpx;
		height:50rpx;
		color:#fff;
		background:#f9436b;
	}
	.xiajia{
		display:flex;
		justify-content: center;
		align-items: center;
		border-radius:12rpx;
		position:absolute;
		right:12rpx;
		bottom:20rpx;
		width:100rpx;
		height:50rpx;
		color:#fff;
		background:#5b88fb;
	}
	page, .content{
		background: #e8e8e8;
		height: 100%;
		box-sizing: border-box;
		padding-top:6rpx;
	}
	
	.swiper-box{
		height: calc(100% - 40px);
	}
	.list-scroll-content{
		height: 100%;
	}
	
	.navbar{
		box-sizing: border-box;
		display: flex;
		height: 40px;
		width:700rpx;
		margin:0 auto;
		margin-top:6rpx;
		border-radius:14rpx;
		padding:12rpx 20rpx;
		
		background: #f7f7f7;
		
		position: relative;
		z-index: 10;
		
		.nav-item{
			border-radius: 14rpx;
			
			flex: 1;
			
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			
			font-size: 15px;
			color: $font-color-dark;
			position: relative;
			
			&.current{
				
				background:#fff;
				color: $base-color;
			}
		}
	}

	.uni-swiper-item{
		height: auto;
	}
	.order-item{
		
		display: flex;
		flex-direction: column;
		padding-left: 30rpx;
		box-sizing: border-box;
		background: #fff;
		margin:0 auto;
		margin-top: 16rpx;
		width:700rpx;
		border-radius:19rpx;
		.i-top{
			display: flex;
			margin-bottom:12rpx;
			
			align-items: center;
			height: 40rpx;
			padding-top:20rpx;
			padding-right:30rpx;
			font-size: $font-base;
			color: $font-color-dark;
			position: relative;
			.time{
				flex: 1;
				color:#8f8f8f;
			}
			.state{
				color: #333;
			}
			.del-btn{
				padding: 10rpx 0 10rpx 36rpx;
				font-size: $font-lg;
				color: $font-color-light;
				position: relative;
				&:after{
					content: '';
					width: 0;
					height: 30rpx;
					border-left: 1px solid $border-color-dark;
					position: absolute;
					left: 20rpx;
					top: 50%;
					transform: translateY(-50%);
				}
			}
		}
		/* 多条商品 */
		.goods-box{
			height: 160rpx;
			padding: 20rpx 0;
			white-space: nowrap;
			.goods-item{
				width: 120rpx;
				height: 120rpx;
				display: inline-block;
				margin-right: 24rpx;
			}
			
		}
		.goods-line{
			&:after{
				position: absolute;
				z-index: 3;
				left: 0;
				right: 20rpx;
				height: 0;
				content: '';
				bottom: 0;
				-webkit-transform: scaleY(0.5);
				transform: scaleY(0.5);
				border-bottom: 1px solid #9e9c9d
			}
		}
		.goods-wrap{
			display:flex;
			padding-top:20rpx;
			position:relative;
			padding-bottom:20rpx;
			
			
		}
		.goods-img{
			border-radius:16rpx;
			display: block;
			width: 180rpx!important;
			height:180rpx!important;
			margin-right:20rpx;
		}
		/* 单条商品 */
		.goods-box-single{
			display: flex;
			padding: 20rpx 0;
			.goods-img{
				display: block;
				width: 120rpx;
				height: 120rpx;
			}
			.right{
				flex: 1;
				display: flex;
				flex-direction: column;
				padding: 0 30rpx 0 24rpx;
				overflow: hidden;
				.title{
					font-size: $font-base + 2rpx;
					color: $font-color-dark;
					line-height: 1;
				}
				.attr-box{
					font-size: $font-sm + 2rpx;
					color: $font-color-light;
					padding: 10rpx 12rpx;
				}
				.price{
					font-size: $font-base + 2rpx;
					color:#333;
					padding-left:20rpx;
					&:before{
						// content: '￥';
						font-size: $font-sm;
						margin: 0 2rpx 0 8rpx;
					}
				}
			}
		}
		
		.price-box{
			display: flex;
			
			align-items: baseline;
			padding: 0rpx 0rpx 20rpx 0rpx;
			font-size: $font-sm + 2rpx;
			color: $font-color-light;
			.num{
				margin: 0 8rpx;
				color: $font-color-dark;
			}
			.price{
				font-size: $font-lg;
				color: #333;
				padding-left:30rpx;
				&:before{
					// content: '￥';
					font-size: $font-sm;
					margin: 0 2rpx 0 8rpx;
				}
			}
		}
		.action-box{
			display: flex;
			justify-content: flex-end;
			align-items: center;
			height: 100rpx;
			position: relative;
			padding-right: 30rpx;
		}
		.action-btn{
			width: 160rpx;
			height: 60rpx;
			margin: 0;
			margin-left: 24rpx;
			padding: 0;
			text-align: center;
			line-height: 60rpx;
			font-size: $font-sm + 2rpx;
			color: $font-color-dark;
			background: #fff;
			border-radius: 100px;
			&:after{
				border-radius: 100px;
			}
			&.recom{
				background: #fff9f9;
				color: $base-color;
				&:after{
					border-color: #f7bcc8;
				}
			}
		}
	}
	
	
	/* load-more */
	.uni-load-more {
		display: flex;
		flex-direction: row;
		height: 80rpx;
		align-items: center;
		justify-content: center
	}
	
	.uni-load-more__text {
		font-size: 28rpx;
		color: #999
	}
	
	.uni-load-more__img {
		height: 24px;
		width: 24px;
		margin-right: 10px
	}
	
	.uni-load-more__img>view {
		position: absolute
	}
	
	.uni-load-more__img>view view {
		width: 6px;
		height: 2px;
		border-top-left-radius: 1px;
		border-bottom-left-radius: 1px;
		background: #999;
		position: absolute;
		opacity: .2;
		transform-origin: 50%;
		animation: load 1.56s ease infinite
	}
	
	.uni-load-more__img>view view:nth-child(1) {
		transform: rotate(90deg);
		top: 2px;
		left: 9px
	}
	
	.uni-load-more__img>view view:nth-child(2) {
		transform: rotate(180deg);
		top: 11px;
		right: 0
	}
	
	.uni-load-more__img>view view:nth-child(3) {
		transform: rotate(270deg);
		bottom: 2px;
		left: 9px
	}
	
	.uni-load-more__img>view view:nth-child(4) {
		top: 11px;
		left: 0
	}
	
	.load1,
	.load2,
	.load3 {
		height: 24px;
		width: 24px
	}
	
	.load2 {
		transform: rotate(30deg)
	}
	
	.load3 {
		transform: rotate(60deg)
	}
	
	.load1 view:nth-child(1) {
		animation-delay: 0s
	}
	
	.load2 view:nth-child(1) {
		animation-delay: .13s
	}
	
	.load3 view:nth-child(1) {
		animation-delay: .26s
	}
	
	.load1 view:nth-child(2) {
		animation-delay: .39s
	}
	
	.load2 view:nth-child(2) {
		animation-delay: .52s
	}
	
	.load3 view:nth-child(2) {
		animation-delay: .65s
	}
	
	.load1 view:nth-child(3) {
		animation-delay: .78s
	}
	
	.load2 view:nth-child(3) {
		animation-delay: .91s
	}
	
	.load3 view:nth-child(3) {
		animation-delay: 1.04s
	}
	
	.load1 view:nth-child(4) {
		animation-delay: 1.17s
	}
	
	.load2 view:nth-child(4) {
		animation-delay: 1.3s
	}
	
	.load3 view:nth-child(4) {
		animation-delay: 1.43s
	}
	
	@-webkit-keyframes load {
		0% {
			opacity: 1
		}
	
		100% {
			opacity: .2
		}
	}
</style>

